#!/usr/bin/env python
# encoding: utf-8


"""
@file: sanjiaohanchuchonghe.py
@time: 2017/3/31 下午3:48
"""
# 三角函数重合
from mathsolver.functions.base import *
from sympy import sin, cos, tan, pi, cot, expand
from mathsolver.functions.sanjiao import sanjiao_utils as su


# 最简单的函数重合问题
# Input paramer1:函数一 Input paramer2: 函数二
class SanJiaoHanShuChongHe01(BaseFunction):
    def solver(self, *args):
        self.label.add('三角函数图像重合得等式')
        y1, f1 = args[0].sympify()
        y2, f2 = args[1].sympify()
        if not (su.trig_is_simped(f1) and su.trig_is_simped(f2)):
            raise Exception('Type Match Error')
        _x = sympify('x')
        _k = sympify('k')
        _2kpi = 2 * _k * pi
        _kpi = _k * pi
        _A1, type1, arg1, b1 = su.simp_trig_info(f1)
        _A2, type2, arg2, b2 = su.simp_trig_info(f2)
        arg1 = expand(arg1)
        arg2 = expand(arg2)
        _w1 = arg1.coeff(_x)
        _h1 = arg1.subs(_x, 0)
        _w2 = arg2.coeff(_x)
        _h2 = arg2.subs(_x, 0)
        # 有可能 A W 都是实数 则A b必须要相等； 否则 就是一个等式组
        if type1 == type2 == sin:
            eq = [_h1 - _h2, _2kpi]
        elif type1 == type2 == cos:
            eq = [_h1 - _h2, _2kpi]
        elif type1 == type2 == tan:
            eq = [_h1 - _h2, _kpi]
        elif type2 == sin and type1 == cos:  # 转换为同名三角函数
            _A1, type1, arg1, b1 = su.cos_2_sin(f1)
            _h1 = arg1.subs(_x, 0)
            eq = [_h1 - _h2, _2kpi]
        elif type2 == cos and type1 == sin:
            _A1, type1, arg1, b1 = su.sin_2_cos(f1)
            _h1 = arg1.subs(_x, 0)
            eq = [_h1 - _h2, _2kpi]
        else:
            raise Exception("to do")
        self.steps.append(['因为函数' + args[0].printing() + '和函数' + args[1].printing(), ''])
        self.steps.append(['所以', BaseEq(eq).printing()])
        self.output.append(BaseEq(eq))
        return self


# 函数重合问题
# Style1 Input paramer1:函数1; paramer2:函数2
class SanJiaoHanShuChongHe02(BaseFunction):
    def solver(self, *args):
        arg1, arg2 = args
        _, f1 = arg1.sympify()
        _, f2 = arg2.sympify()
        _x = sympify('x')
        _k = sympify('k')
        _2kpi = 2 * _k * pi
        _A1, t1, arg1, b1 = su.simp_trig_info(f1)
        if t1 in (tan, cot):
            _A1, t1, arg1, b1 = su.cot_2_tan(f1)
            _A2, t2, arg2, b2 = su.cot_2_tan(f2)
            if _A1 != _A2 or _A1 < 0 or _A2 < 0:
                _A1, t1, arg1, b1 = su.tan_2_cot(f1)
                _A2, t2, arg2, b2 = su.tan_2_cot(f2)
        else:
            _A1, t1, arg1, b1 = su.cos_2_sin(f1)
            _A2, t2, arg2, b2 = su.cos_2_sin(f2)
            if _A1 != _A2 or _A1 < 0 or _A2 < 0:
                _A1, t1, arg1, b1 = su.sin_2_cos(f1)
                _A2, t2, arg2, b2 = su.sin_2_cos(f2)
        w1 = arg1.coeff(_x)
        w2 = arg2.coeff(_x)
        _h1 = arg1.subs(_x, 0)
        _h2 = arg2.subs(_x, 0)
        # 只有可能 b 和 h中可能含有参数
        if _A1 == _A2 and _A1 > 0:
            eqs = []
            if t1 in (sin, cos):
                eqs.append([w1, w2])
                eqs.append([b1, b2])
                eqs.append([_h1 - _h2, _2kpi])
            else:
                eqs.append([w1, w2])
                eqs.append([b1, b2])
                eqs.append([_h1 - _h2, _2kpi / 2])
        else:
            raise Exception('Can not solve the question')
        self.output.append(BaseEqs(eqs))
        return self


class SanJiaoHanShuChongHe(BaseFunction):
    CLS = [SanJiaoHanShuChongHe01, SanJiaoHanShuChongHe02, ]

    def solver(self, *args):
        solve_f = None
        for cl in SanJiaoHanShuChongHe.CLS:
            try:
                solve_f = cl()
                solve_f.known = self.known
                solve_f = solve_f.solver(*args)
                solve_f.label.add('三角函数图像重合得等式')
                break
            except Exception:
                solve_f = None
        if not solve_f:
            raise Exception('Try Error')
        return solve_f
